﻿using NineSeas.Models;
using NineSeas.ViewModels;
using Orchard;
using Orchard.Mvc;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Services;
using Orchard.Users.Models;
using System.Web.Mvc;
using System;
using Orchard.FileSystems.Media;
namespace NineSeas.Controllers
{
    public class AuthorProfileController : Controller, IUpdateModel
    {
        private readonly IAuthenticationService _authenticationService;
        private readonly IOrchardServices _services;
        private readonly IClock _clock;
        private readonly IStorageProvider _storageProvider;
        private Localizer T { get; set; }

        public AuthorProfileController(IOrchardServices services, IAuthenticationService authenticationService,
                IClock clock,
                IStorageProvider storageProvider)
        {
            this._services = services;
            this._authenticationService = authenticationService;
            this._clock = clock;
            this._storageProvider = storageProvider;
            T = NullLocalizer.Instance;
        }

        public ActionResult EditProfile()
        {
            var user = _authenticationService.GetAuthenticatedUser();
            if (_authenticationService.GetAuthenticatedUser() == null)
                throw new OrchardSecurityException(T("Login required"));

            var profile = user.As<AuthorProfilePart>();

            return new ShapeResult(this, _services.New.Authoring_EditProfile(
                    Profile: profile,
                    AvatarUrl: String.IsNullOrWhiteSpace(profile.AvatarUrl) ?
                                Url.Content("~/Modules/NineSeas/Images/placeholder-photo.png")
                                : profile.AvatarUrl,
                    User: user
                ));
        }

        [HttpPost, ActionName("EditProfile")]
        public ActionResult EditProfilePost(AuthorProfileViewModel model)
        {
            var user = _authenticationService.GetAuthenticatedUser();
            if (_authenticationService.GetAuthenticatedUser() == null)
                throw new OrchardSecurityException(T("Login required"));

            user.As<UserPart>().Email = model.Email;

            var profile = user.As<AuthorProfilePart>();

            if (profile != null)
            {
                profile.DisplayName = model.DisplayName;
                profile.StatusMessage = model.StatusMessage;
                profile.Phone = model.Phone;
                profile.BannerColor = model.BannerColor;

                profile.DateOfBirth = model.DateOfBirth;

                //profile.AvatarUrl = model.AvatarUrl;
                if (!String.IsNullOrEmpty(model.AvatarStoragePath)
                    && String.Compare(profile.AvatarStoragePath, model.AvatarStoragePath) != 0)
                {
                    profile.AvatarUrl = _storageProvider.GetPublicUrl(model.AvatarStoragePath);
                }

                profile.AvatarStoragePath = model.AvatarStoragePath;
            }


            _services.ContentManager.UpdateEditor(profile, this);

            return this.Json(new { success = true });
        }

        bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties)
        {
            return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
        }

        void IUpdateModel.AddModelError(string key, LocalizedString errorMessage)
        {
            ModelState.AddModelError(key, errorMessage.ToString());
        }
    }
}